package com.luming.common.core.domain;

import com.luming.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

/**
 * @Author Luming
 * @Date 2024/8/27 17:32
 * @Description:
 */
@Getter
@Setter
public class R<T> {
    
    /**
     * 消息状态码
     */
    private int code;
    
    /**
     * 消息内容
     */
    private String msg;
    
    /**
     * 数据对象
     */
    private T data;
    
    //成功，不返回数据
    public static <T> R<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }
    
    //成功，返回数据
    public static <T> R<T> ok(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }
    
    //默认失败
    public static <T> R<T> fail() {
        return assembleResult(null, ResultCode.FAILED);
    }
    
    //指定错误码
    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }
    
    public static R<?> fail(int code, String msg) {
        return assembleResult(code, msg, null);
    }
    
    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setData(data);
        r.setMsg(resultCode.getMsg());
        return r;
    }
    
    private static <T> R<T> assembleResult(int code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
    

}
